;(function($){
	$.fn.spa_search_table = function(opts){
		var el = this;
		var opt = {
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	};
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.getShoupai();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//返回
			el.find("#close-dialog,#modalBtnCancel").unbind("click").click(function(){
				el.modal("hide");
				param.closeBack();
				el.empty();
			});
			
			
			//台号搜索
			el.find("#searchTh").unbind("keyup").keyup(function(e){
				var value = $(this).val();
				el.find("#shouPaiContainer").find(".box[data-body-bh]").addClass("hide");
				el.find("#shouPaiContainer").find(".floorTitle").parent().show();
				if(value){
					el.find("#shouPaiContainer").find(".box[data-body-bh^="+value+"]").removeClass("hide");
				}else{
					el.find("#shouPaiContainer").find(".box[data-body-bh]").removeClass("hide");
				}
				
				//判断层是否显示
				el.find("#shouPaiContainer").find(".floorLine").each(function(k,v){
					var size = $(v).find(".box:visible").size();
					if(size==0){
						$(v).closest(".floorLine").parent().hide();
					}
				})
			})
			
			//清空按钮
			el.find("#btnModalClear").unbind("click").click(function(){
				el.find("#searchTh").val("");
				el.find("#shouPaiContainer").find(".box[data-body-bh]").removeClass("hide");
				el.find("#shouPaiContainer").find(".floorTitle").parent().show();
				
			});
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="mykb-box" id="mykeyboard"></div>'+
						'<div id="modal-dialog" class="modal-dialog" style="width:800px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="title-dialog">'+$.i18n.propJsp("选台")+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="min-height: 100px;">'+
									'<div class="well well-sm">'+
										'<div class="input-group" style="width:350px;">'+
											'<input type="text" class="form-control search-query input_all" placeholder="" name="searchTh" id="searchTh" value="">'+
											'<span class="input-group-btn">'+
												'<button type="button" class="btn btn-primary btn-sm" id="btnModalClear">'+
													'<span class="ace-icon fa fa-trash"></span>'+
													$.i18n.propJsp("清空")+
												'</button>'+
											'</span>'+
										'</div>'+
									'</div>'+
									'<div id="shouPaiContainer" class="widget-box" style="height:400px;overflow-y:auto;overflow-x:hidden;">'+
										
									'</div>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancel" type="button" class="btn btn-sm btn-default">'+
													'<i class="ace-icon fa fa-undo"></i>'+
													$.i18n.propJsp("返回")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		getShoupai : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			t.wait();
			jQuery.ajax({  
	    		type : 'POST',
	    		url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",  
	    		data : $.HSAPI_ASKJSONINFO(JSON.stringify({}),'SPAShoupaiOpenDefault','{}'),
	    		dataType : 'json',  
	    		success : function(data){
					t.goon();
	    			if(data.RETURN==1){
	    				param.ct_th = data.ct_th;
	    				//初始手牌
	    				t.initShoupaiHtml(data.ct_th);
	    			}else{
						if(data.hasOwnProperty("gritter")){
							if(data.hasOwnProperty("gritterstyle"))
								jf_successDelayed(data.gritter,2000,data.gritterstyle);
							else
								jf_successDelayed(data.gritter,2000); 
						}else{
		    				jf_alert(data.MSG)
						}
					}
	    		},  
	    		error : function(data){
					t.goon();
	    			jf_alert(JSON.stringify(data));
	    		}  
	    	});
		},
		initShoupaiHtml : function(ds){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var floor="";
    		var itemDom=el.find("#shouPaiContainer"); 
    		itemDom.empty();
    		
    		var queryt = "select * from json order by floor";
    		ds = jsonsql.query(queryt,ds);
    		var html = ""
    		
    		var floor = 'startFor000';//用来区分是否是第一个循环
    		ds.map(x => {
    			
    			if(floor=='startFor000'){
    				floor = x.floor;
    				html = '<div style="display:flex !important;margin-bottom:3px;">'+
								'<div class="floorTitle" style="width:40px;display:flex;flex-direction:column;justify-content:center;align-items:center;font-size:16px;font-weight:bold;background:#b5d1e2;border-bottom: 1px solid #f5f5f5;">'+
								x.floor +
							'</div>'+
							'<div class="floorLine clearfix" id="floor-1" style="flex:1;">';
    			}else if(floor!=x.floor){
    				floor = x.floor;
    				html +=		'</div>'+
    						'</div>'+
    						'<div style="display:flex !important;margin-bottom:3px;">'+
	    						'<div class="floorTitle" style="width:40px;display:flex;flex-direction:column;justify-content:center;align-items:center;font-size:16px;font-weight:bold;background:#b5d1e2;border-bottom: 1px solid #f5f5f5;">'+
	    							floor +
	    						'</div>'+
	    						'<div class="floorLine clearfix" id="floor-1" style="flex:1;">';
    			}
    			
    			var	xfje=x.hasOwnProperty("xfje")?x.xfje:"";
    			var dfje=(x.hasOwnProperty("dfje")&&x.dfje>0.00)?"("+x.dfje+")":"";
    			
    			var handpanel_info={};
    			if(x.hasOwnProperty("handpanel_info")&&x.handpanel_info!="")
    				handpanel_info=JSON.parse(x.handpanel_info);
    			
    			var ico_info=[]
    			var ico_info_text="";
    			if(x.hasOwnProperty("ico_info")&&x.ico_info!="[]")
    				ico_info=JSON.parse(x.ico_info);
    			$.each(ico_info,function(key,value){ 
    				ico_info_text += getTextStyle(value); 
				})

				var height="60px";
				if(handpanel_info.hasOwnProperty("height"))
					height=handpanel_info.height;
				var width="100px";
				if(handpanel_info.hasOwnProperty("width")) 
					width=handpanel_info.width;
				var border="";
				if(handpanel_info.hasOwnProperty("border")&&handpanel_info.border>"0"&&handpanel_info.border!="")  
					border="border: "+handpanel_info.border+"px solid #cdd8e3;";
				var padding="padding: 0px;";
				if(handpanel_info.hasOwnProperty("padding")&&handpanel_info.padding!="")      
					padding="padding: "+handpanel_info.padding+"px;";
					
    			html +='<div class="box" data-body-floor="'+x.floor+'" data-bgcolor="'+handpanel_info.bgcolor+'" data-body-bh="'+x.bh+'" style="'+border+' background-color:'+handpanel_info.bgcolor+';width:'+width+' !important; height:'+height+' !important; '+padding+' float:left;margin-left:5px;margin-top:5px;">'+
						     '<span class="json hide">'+
						     	JSON.stringify(x)+
						     '</span>'+
    						'<div style="width: 100%;height:33%;float: left;"><span style="color:'+handpanel_info.tcolor+'">'+x.bh+'</span>'+ 
						      	'<small class="text-muted" style="color:'+handpanel_info.tcolor+' !important; font-size:60%;  float:right;">&nbsp;'+ico_info_text+ '</small>'+
						     '</div>'+
						     '<div style="width: 100%;height:33%;float: left;">'+ 
 					      	 	'<span style="float:right;color:'+handpanel_info.tcolor+'">'+xfje+'</span>'+  
 						     '</div>'+
						     '<div style="width: 100%;height:33%;float: left;white-space:nowrap; overflow:hidden; text-overflow:ellipsis;">'+  
						     	'<span style="float:right;color:'+handpanel_info.tcolor+'">'+dfje+'</span>'+  
				      	 	'</div>'+
					     '</div>';
    		});
    		
    		html += '</div></div>';
    		itemDom.html(html);
    		
    		//选择手牌 
			el.find("#shouPaiContainer").find(".box").unbind("click").click(function(){
				$(this).hide();
				var data = $.parseJSON($(this).find("span.json").text());
				el.modal("hide");
				el.empty();
				param.dataBack(data.bh);
			})
		},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
	}
	
	function checkTwoPoint(value){  		
	    value=value.toString()+".00";
	    value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
	    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
	    return value;
	}
	
})(jQuery);